// pages/doctordetails/doctordetails.js
let wxparse = require("../../wxParse/wxParse.js");
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myHttp: app.globalData.myHttp,
    doctordetailslist: [],
    doctorscheduleList: [],
    selectyisheng: '',
    isShowModel: false,//控制弹窗是否显示，默认不显示
    isShowConfirm: false,//是否只显示确定按钮，默认不是
    ModelId: 0,//弹窗id
    ModelTitle: '',//弹窗标题
    readtimes:'',
    isDisabled: true
  },
  //阻断模态弹窗点击穿透
  preventTouchMove: function(){},
  //调用模态弹窗
  showModel: function (e){
    //将传过来的标题和内容展示到弹窗上
    this.setData({
      isShowModel: true,
      ModelId: e.ModelId,
      ModelTitle: e.ModelTitle,
    })
  },
  //确定事件
  confirm: function(e){
    if (e.currentTarget.dataset.modelid == 0) {
      console.log("用户点击了我已阅读")
    }
     //关闭模态弹窗
     this.setData({
      isShowModel: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(decodeURIComponent(options.selectyisheng))
    var that = this;
    var doctordetailslist = '';
    var user = wx.getStorageSync("user")
    this.setData({
      selectyisheng: JSON.parse(decodeURIComponent(options.selectyisheng))
    });
    wx.showLoading({title: '数据加载中', icon: 'loading'});
    wx.request({
      url: this.data.myHttp + '/hospital/doctorInfo',
      data: {
        doctorId: this.data.selectyisheng.id,
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      success: (res) => {
        if (res.obj !== null) {
          doctordetailslist = res.data.obj[0] 
          var ModelContent = doctordetailslist.description || doctordetailslist.explain;
          if (ModelContent) {
            wxparse.wxParse('ModelContent', 'html', ModelContent, that, 5)
            this.showModel({
              ModelId: 0,
              ModelTitle: '挂号须知'
            })
            //倒计时
            this.readCount(10)
          }
          that.setData({
            doctordetailslist: res.data.obj[0], //将表中查询出来的信息传给list          
          })
          if (res.data.obj[0].scheduleList !== null) {
            console.log(res.data.obj[0].scheduleList)
            this.setData({
              doctorscheduleList: res.data.obj[0].scheduleList,
            })
          }
        }else{
          doctordetailslist = new Object();
          console.log(that.data.selectyisheng);
          doctordetailslist = that.data.selectyisheng;
          doctordetailslist.imgUrl = that.data.selectyisheng.logo;
          that.setData({
            doctordetailslist: doctordetailslist
          })
        }
      },complete: () => {
         wx.hideLoading()
      }
    })
  },

  //图片加载失败使用同一图片代替
  binderrorimg: function(e) {
    var errorImgIndex = e.target.dataset.errorimg //获取循环的小标
    var imgObject = "doctordetailslist.imgUrl" //carlistData为数据源，对象数组
    var errorImg = {}
    errorImg[imgObject] = "../../image/user.png" //我们构建一个对象
    this.setData(errorImg) //修改数据源对应的数据
  },

  bindyuyue: function(event) {
    var user = wx.getStorageSync("user")
    var index = event.currentTarget.dataset.bindex;
    if (user == null || user == '' || user == undefined) {
      wx.showModal({
        title: '提示',
        content: '预约前请先登录',
        confirmText: '登录',
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
            wx.navigateTo({
              url: '../login/login',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      if (!this.data.doctorscheduleList[index].reservation) {
        this.toast('预约已满');
        return false;
      }
      wx.navigateTo({
        url: '../appointmentInformation/appointmentInformation?doctorAppointmentInf=' + encodeURIComponent(JSON.stringify(this.data.doctordetailslist)) + '&appointmentDayTimeNum=' + index
      })
    }
  },
  
  //10秒倒计时
  readCount(time){
    if (time >= 0) {
      this.setData({
        readtimes: time,
      })
      setTimeout(this.readCount, 1000, time - 1)
    }else{
      this.setData({
        isDisabled: false
      })
    }
  },

  toast(img) {
    wx.showToast({
      title: img,
      icon: 'none',
      duration: 2000
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})